package com.guotd.jspwork2.controller;
import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static com.guotd.jspwork2.utils.JDBCUtil.getConnection;

@WebServlet("/saveTV")
public class saveTVServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name=request.getParameter("name");
        double price= Double.parseDouble(request.getParameter("price"));
        int HZ=Integer.parseInt(request.getParameter("hz"));
        String kind=request.getParameter("kind");
        String sql="insert into tv(id,name,price,hz,kind) values(?,?,?,?,?)";
        String []args={id,name, String.valueOf(price), String.valueOf(HZ),kind};
        int row = JDBCUtil.update(sql, args);
        if (row > 0) {
            request.getSession().setAttribute("tv_msg", "保存成功");
            request.getSession().setAttribute("kind", kind);
            request.getRequestDispatcher("getAllTVs").forward(request, response);
        }else{
            request.setAttribute("tv_msg","保存失败");
        }
    }
}
